import os,sys

from django import test
from django import template

from PIL import Image, ImageFile

from imaging import image_open, resize, resize_file
from templatetags.quick_tag import re_kw, QuickTagNode


class TestHTML(test.TestCase):
    
    def setUp(self):
        pass
        

def plain(string):
    return string.replace('\n','').replace('  ','').replace('\t',' ')


class TestQuicktag(test.TestCase):
    
    def testFindKeyword(self):
        self.assertFalse(re_kw.match('nome.teste'))
        m = re_kw.match('nome=teste')
        self.assertTrue(m)
        self.assertEqual('nome',m.group(1))
        self.assertEqual('teste',m.group(2))

        
    def testResolveKeyword(self):
        f = lambda : ''
        node = QuickTagNode(f,['teste','nome=teste'])
        self.assertEqual('nome',node.kwargs.keys()[0])
        self.assertEqual(1,len(node.args))



class TestHtmlMedia(test.TestCase):

    ext_list = ['jpg','bmp','png','gif','css','swf']
    
    def getpath(self,ext):
        return os.path.join(self.base_path,'%s.%s' % (self.base_name,ext))

    def createTestImage(self,ext):
        img = Image.new('RGB',(1,1))
        img.save(self.getpath(ext))
        setattr(self,ext,img)
        
    def createTestFile(self,ext):
        f = open(self.getpath(ext),'w')
        f.close()


    def html_media_template(self,extension):
        snippet = '{%% load html %%}{%% html_media "%s.%s" %%}' % (self.base_name,extension)
        return self.render_template(snippet)
    
    def render_template(self,snippet):
        return template.Template(snippet).render({})

    def setUp(self):
        from django.conf import settings
        try:
            self.base_path = settings.MEDIA_ROOT
        except:
            raise Exception, 'For this test, MEDIA_ROOT must be set on settings.py'
        self.base_name = 'teste'
    
    def tearDown(self):
        for extension in self.ext_list:
            path = os.path.join(self.base_path,'%s.%s'%(self.base_name,extension))
            if os.path.exists(path):
                os.remove(path)

    def testWrongFile(self):
        self.assertEqual('',self.html_media_template('notexist'))
        
        snippet = '{% load html %}{% html_media "none.jpg" ignore_invalid_path=1 width="200" height="200" %}'
        self.assertEqual('<img src="/media/none.jpg" width="200" height="200"   />',self.render_template(snippet))
        
    def testWrongExtension(self):
        snippet = '{% load html %}{% html_media "teste.none" width="200" height="200" %}'
        self.assertEqual('',self.render_template(snippet))
        
    def testJPG(self):
        self.createTestImage('jpg')
        self.assertEqual('<img src="/media/teste.jpg',self.html_media_template('jpg')[:26])
    
    def testPNG(self):
        self.createTestImage('png')
        self.assertEqual('<img src="/media/teste.png',self.html_media_template('png')[:26])
        
    def testBMP(self):
        self.createTestImage('bmp')
        self.assertEqual('<img src="/media/teste.bmp',self.html_media_template('bmp')[:26])
        
    def testCSS(self):
        self.createTestFile('css')
        self.assertEqual('<link rel="stylesheet" href="/media/teste.css',self.html_media_template('css')[:45])
        
    def testSWF(self):
        self.createTestFile('swf')
        default = '<script type="text/javascript">\nAC_FL_RunContent( \'codebase\',\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\',\'wmode\', \'opaque\',\'width\',\'\',\'height\',\'\',\'src\',\'/media/teste'
        self.assertEqual(default,self.html_media_template('swf')[:208])
        
    def testForm(self):
        pass